package com.qdlc.p2p.dal.dao.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.ExperienceDao;
import com.qdlc.p2p.dal.dto.Experience;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.ExperienceModel;
import com.qdlc.p2p.dal.mybatis.model.mapper.ExperienceMapper;
import com.qdlc.p2p.dal.mybatis.pager.PageProxyBuilder;
import com.qdlc.p2p.dal.mybatis.pager.core.PageParameter;

/**
 * 体验券信息DAO接口
 * @author xxx
 * @version 2.0
 * @since 2015年04月23日 10时46分28秒
 */
@Repository(value = "experienceDao")
public class ExperienceDaoImpl extends BaseDaoImpl<Experience> implements ExperienceDao {

	@Resource
	ExperienceMapper experienceMapper;
	
    @Override
	public int countUsableExperience(long userId) {
    	int count = experienceMapper.countUsableExperience(userId);
		return count;
	}
    
	@Override
	public List<Experience> findUsableExperience(long userId) {
		List<Experience> list = experienceMapper.findUsableExperience(userId);
		return list;
	}

	@Override
	public int updateStatus(long id, byte status, byte preStatus) {
		int count = experienceMapper.updateStatus(id, status, preStatus);
		return count;
	}

    @Override
    public double sumAmount(long[] ids, long userId) {
        if (ids == null || ids.length == 0) {
            return 0;
        }
        Double amount = experienceMapper.sumAmount(ids, userId, ExperienceModel.EXPERIENCE_STATUS_UNUSED);
        return amount == null ? 0.0D : amount;
    }

	@Override
	public Object[] getCapitalAndInterestByBorrowAndPeriod(long borrowId, int period) {
		String jpql = "select sum(capital), sum(interest) from ExperienceCollection ec where ec.status=0 and ec.borrow.id= ?1 and ec.period= ?2 ";
		Query query = em.createQuery(jpql).setParameter(1, borrowId).setParameter(2, (byte)period);
        Object[] capitalAndInterest = null;
        Object obj = query.getSingleResult();
        if (obj != null) {
        	capitalAndInterest = (Object[]) obj;
        	if (null ==capitalAndInterest[0]) {
        		capitalAndInterest[0]=0.0;
			}
        	if (capitalAndInterest[1]==null) {
        		capitalAndInterest[1]=0.0;
			}
        }
        return capitalAndInterest;
	}

	/**
	 * 体验券过期处理
	 */
	@Override
	public void eperienceExpiredDispose() {
		//status=0的数据，当experience_time 小于当前时间时，将status修改为-1
		experienceMapper.experienceExpiredDispose();
	}

	@Override
	public boolean checkExperience(long[] experienceIds, User user) {
		if (experienceIds == null) {
			return false;
		}
		if(experienceIds.length == 0){
			return true;
		}
		
		int count = experienceMapper.checkExperience(experienceIds, user.getUserId(), ExperienceModel.EXPERIENCE_STATUS_UNUSED);
		// 如果查询结果集个数不等于用户所选择的体验券ID个数，则代表用户存在恶意修改表单提交ID的嫌疑，所以需要进行处理
		if (count == experienceIds.length) {
			return true;
		}
		return false;
	}

	@Override
	public double getExperienceSumByStatus(int status, String date) {
		Double amount = experienceMapper.getExperienceSumByStatus(status, date);
		return amount == null ? 0.0D : amount;
	}

	@Override
	public PageDataList<ExperienceModel> findUserList(ExperienceModel model) {
		long userId = model.getUserId();
		int status = model.getStatus();
		int currentPage = model.getPage();
		int pageSize = model.getRows();
		PageProxyBuilder<ExperienceMapper> proxy = PageProxyBuilder.newBuilder(ExperienceMapper.class);
		proxy.setPageParameter(currentPage, pageSize);
		List<ExperienceModel> list = proxy.createProxy().findUserList(userId, status);
		PageParameter pageParameter = proxy.getPageParameter();
		PageDataList<ExperienceModel> pageDataList = new PageDataList<ExperienceModel>(pageParameter.convert2Page(), list);
		return pageDataList;
	}

	/**
	 * 
	 * @param model
	 * @return
	 */
	@Override
	public PageDataList<ExperienceModel> findByModel(ExperienceModel model) {
		model.setStartAndEndDate();
		PageProxyBuilder<ExperienceMapper> proxy = PageProxyBuilder.newBuilder(ExperienceMapper.class);
		proxy.setPageParameter(model.getPage(), model.getRows());
		List<ExperienceModel> list = proxy.createProxy().findByModel(model);
		PageParameter pageParameter = proxy.getPageParameter();
		PageDataList<ExperienceModel> pageDataList = new PageDataList<ExperienceModel>(pageParameter.convert2Page(), list);
		return pageDataList;
	}
	
	/**
	 * 根据用户名模糊查询
	 * @param userName
	 * @return
	 */
	public PageDataList<ExperienceModel> findByUserName(String userName, int currentPage, int pageSize){
		PageProxyBuilder<ExperienceMapper> proxy = PageProxyBuilder.newBuilder(ExperienceMapper.class);
		proxy.setPageParameter(currentPage, pageSize);
		List<ExperienceModel> list = proxy.createProxy().findByUserName(userName);
		PageParameter pageParameter = proxy.getPageParameter();
		PageDataList<ExperienceModel> pageDataList = new PageDataList<ExperienceModel>(pageParameter.convert2Page(), list);
		return pageDataList;
	}
	
}
